package com.gccloud.dataroom.core.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gccloud.dataroom.service.PermissionServiceImpl;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;

//import static com.mes.common.security.utils.SecurityUtils.getUsername;

@Configuration
@Component
public class DataRoomMetaObjectHandler implements MetaObjectHandler {

    /**
     * 创建时间字段
     */
    private static final String CREATE_DATE = "createDate";
    /**
     * 更新时间字段
     */
    private static final String UPDATE_DATE = "updateDate";

    /**
     * 更新时间字段
     */
    @Autowired
    private PermissionServiceImpl permissionService;

    @Override
    public void insertFill(MetaObject metaObject) {

        Date date = new Date();
        System.out.println("DataRoomMetaObjectHandler.insertFill 已启动！");
        this.strictInsertFill(metaObject, "createBy", String.class, permissionService.getUserName());
        this.strictInsertFill(metaObject, "updateBy", String.class, permissionService.getUserName());
        this.setFieldValByName(CREATE_DATE, date, metaObject);
        this.setFieldValByName(UPDATE_DATE, date, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "updateBy", String.class, permissionService.getUserName());
        this.setFieldValByName(UPDATE_DATE, new Date(), metaObject);
    }
}

